package com.bite.springblog.controller;

import com.bite.springblog.pojo.request.BlogInfoRequest;
import com.bite.springblog.pojo.request.UpdateBlogRequest;
import com.bite.springblog.pojo.response.BlogResponse;
import com.bite.springblog.pojo.response.Result;
import com.bite.springblog.service.BlogService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Resource(name = "blogServiceImpl")
    private BlogService blogService;

//    @Autowired
//    private BlogServiceImpl blogService;

    @RequestMapping("/getbloglist")
    public Result getBlogList(){
        log.info("获取博客列表..");
        List<BlogResponse> blogResponseList = blogService.getBolgList();
        for (int i = 0; i < blogResponseList.size(); i++) {
            if(blogResponseList.get(i).getContent().length()>=50){
                blogResponseList.get(i).setContent(blogResponseList.get(i).getContent().substring(0,51)+"...");
            }
        }
        return Result.success(blogResponseList);
    }

    @RequestMapping("/getblogdetail")
    public Result getBlogDetail(@NotNull String blogId){
        log.info("获取博客详情..");
        BlogResponse blogDetail = blogService.getBlogDetail(blogId);
        if(blogDetail == null){
            return Result.fail("博客不存在!");
        }
        return Result.success(blogDetail);
    }

    //新增博客
    @RequestMapping("/add")
    public boolean addBlog(@RequestBody @Validated BlogInfoRequest blogInfoRequest){
        return blogService.addBolg(blogInfoRequest);
    }

    //更新博客
    @RequestMapping("/update")
    public boolean updateBlog(@RequestBody @Validated UpdateBlogRequest updateBlogRequest){
        return blogService.updateBlog(updateBlogRequest);
    }

    //删除博客
    @RequestMapping("/delete")
    public boolean deleteBlog(@NotNull String blogId){
        return blogService.deleteBlog(blogId);
    }
}





